package com.ruoyi.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允许认证
        //config.addAllowedOrigin("*"); // SpringBoot 升级2.6 之前 允许任何域进行跨域访问
        config.addAllowedOriginPattern("*");//SpringBoot 升级2.6后
        config.addAllowedHeader("*"); // 允许任何头进行跨域访问
        config.addAllowedMethod("*"); // 允许任何方法进行跨域访问，如：POST、GET等
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}